package com.yenroc.ho.多线程;

public class MyThread01 {

    public static void main(String[] args) throws InterruptedException {
        System.out.print(Thread.currentThread().getName() + "\tmain running..\n");

        // 线程类start 执行线程类的run 方法
        Thread t = new MyThread();
        // t.setDaemon(true); // 把该线程标记为守护线程
        t.start();// 启动一个线程

        // java 8 lambda 表达式简写
        Thread t2 = new Thread(()->{
            System.out.print(Thread.currentThread().getName() + "\tlambda Thread running..\n");
            try {
                Thread.sleep(100L);
            } catch (java.lang.InterruptedException e){

            }
            System.out.print(Thread.currentThread().getName() + "\tlambda Thread end..\n");
        });
        t2.start();

        // 执行实现Runnable类。
        Thread t3 = new Thread(new MyRunnable());
        t3.start();

        t2.join();// 只有当t2 线程执行完，main 线程才会继续
        // t.interrupt(); // 中断t线程

        System.out.print(Thread.currentThread().getName() + "\tmain end..\n");

    }

    /**
     * 线程的状态:
     *  New: 新建
     *  Runnable ： 运行中的线程
     *  Blocked： 运行中的线程，因为某些操作被阻塞挂起
     *  waiting： 运行中的线程，因为某写操作在等待中
     *  Timed Waiting：运行中的线程，因为执行sleep()方法正在计时等待
     *  Terminated： 终止的线程
     */
    private void threadStatus() {}
}

// 1.继承Thread 类，重写run方法
class MyThread extends Thread {
    @Override
    public void run() {
        System.out.print(Thread.currentThread().getName() + "\tMyThread class is running..\n");
        try {
            Thread.sleep(500L);
        } catch (java.lang.InterruptedException e){

        }
        System.out.print(Thread.currentThread().getName() + "\tMyThread class is end..\n");
    }
}

// 2. 实现Runnable接口，
class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.print(Thread.currentThread().getName() + "\tMyRunnable class is running..\n");
        try {
            Thread.sleep(500L);
        } catch (java.lang.InterruptedException e){

        }
        System.out.print(Thread.currentThread().getName() + "\tMyRunnable class is end..\n");
    }
}
